package com.example.testvideoview;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		VideoView myVideoView = (VideoView) findViewById(R.id.video);

		String viewSource = "android.resource://com.example.testvideoview/" + R.raw.my_live;
		//String viewSource = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQklThqIVp_AsxMYESARFEIJbXYtZ29vZ2xlSARSBWluZGV4YIvJo6nmx9DvSww=/0/0/0/video.3gp";

		myVideoView.setVideoURI(Uri.parse(viewSource));
		myVideoView.setMediaController(new MediaController(this));

		myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
		myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
		myVideoView.setOnErrorListener(myVideoViewErrorListener);

		myVideoView.requestFocus();
		myVideoView.start();
	}

	MediaPlayer.OnCompletionListener myVideoViewCompletionListener = new MediaPlayer.OnCompletionListener() {

		@Override
		public void onCompletion(MediaPlayer arg0) {
			Toast.makeText(MainActivity.this, "End of Video", Toast.LENGTH_LONG)
					.show();
		}
	};

	MediaPlayer.OnPreparedListener MyVideoViewPreparedListener = new MediaPlayer.OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer arg0) {
			Toast.makeText(MainActivity.this,
					"Media file is loaded and ready to go", Toast.LENGTH_LONG)
					.show();

		}
	};

	MediaPlayer.OnErrorListener myVideoViewErrorListener = new MediaPlayer.OnErrorListener() {

		@Override
		public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
			Toast.makeText(MainActivity.this, "Error!!!", Toast.LENGTH_LONG)
					.show();
			return true;
		}
	};

}
